﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;

namespace Workflow.BaoJia
{

    public partial class Add : System.Web.UI.Page
    {
        private BaojiaManager objBaojiaManager = new BaojiaManager();
        private UserTeamRoleManager objUserTeamRoleManager = new UserTeamRoleManager(); 
        int workParentId;
        int number;
        private static string comDate;
        private static string no;

        protected void Page_Load(object sender, EventArgs e)
        {
            workParentId = Convert.ToInt32(Request.QueryString["WorkParentId"]);
            number = Convert.ToInt32(Request.QueryString["No"]);
            if (!IsPostBack)
            {
                try
                {
                    Baojia objBaojia = objBaojiaManager.GetBaojiaInfo(workParentId);
                    if (objBaojia != null)
                    {
                        no = objBaojia.Number;
                        txtNo.Text = objBaojia.Number;
                        foreach (var item in Enum.GetValues(typeof(Customer)))
                        {
                            if(objBaojia.Customer==item.ToString())
                            {
                                ddlCustomer.SelectedValue = objBaojia.Customer;
                                otherCustmoer.Style.Value = "display:none";
                                break;
                            }
                            else
                            {
                                ddlCustomer.SelectedValue = "其他";
                                txtCustomer.Text = objBaojia.Customer;
                                otherCustmoer.Style.Value = "";
                            }
                        }
                        
                        //txtCustomer.Text = objBaojia.Customer;
                        txtRequirement.Text = objBaojia.Requirement.ToString();
                        txtSales.Text = objBaojia.Sales.ToString();
                        ddlClass.SelectedValue = objBaojia.Class;
                        //ddlType.SelectedValue = objBaojia.CostType;
                        txtType.Text = objBaojia.OtherCostType.ToString();
                        ddlSuccess.SelectedValue = objBaojia.Success.ToString();
                        txtMoldRequire.Text = objBaojia.MoldRequire.ToString();
                        ddlMoldClass.SelectedValue = objBaojia.MoldClass;
                        comDate = objBaojia.CompleteTime.ToString("yyyy-MM-dd");
                        if (comDate != "1900-01-01")
                        {
                            string str = string.Format("<script>document.getElementById('txtCompleteTime').value = '{0}';</script>", comDate);
                            Page.ClientScript.RegisterStartupScript(Page.GetType(), "", str);
                        }
                        txtPs.Text = objBaojia.Ps.ToString();
                        string[] strs = objBaojia.CostType.Split(',');
                        for (int i = 0; i < strs.Length; i++)
			            {
			                if (strs[i] == "新模报价") { cblType.Items[0].Selected = true; continue; }
                            if (strs[i] == "注塑报价") { cblType.Items[1].Selected = true; continue; }
                            if (strs[i] == "装配报价") { cblType.Items[2].Selected = true; continue; }
                            if (strs[i] == "改模报价") { cblType.Items[3].Selected = true; continue; }
                            if (strs[i] == "备件报价") { cblType.Items[4].Selected = true; continue; }
                            if (strs[i] == "外购件报价") { cblType.Items[5].Selected = true; continue; }
                            if (strs[i] == "冲压报价") { cblType.Items[6].Selected = true; continue; }
			            }
                    }
                    else
                    {
                        
                        txtNo.Text = "";
                        txtCustomer.Text = "";
                        txtRequirement.Text = "";
                        txtSales.Text = "";
                        ddlClass.SelectedIndex = 0;
                        cblType.SelectedIndex = -1;
                        txtType.Text = "";
                        ddlSuccess.SelectedIndex = 0;
                        ddlCustomer.SelectedIndex = 0;
                        otherCustmoer.Style.Value = "display:none";
                        txtMoldRequire.Text = "";
                        ddlMoldClass.SelectedIndex = 0;
                        txtPs.Text = "";
                        string str = string.Format("<script>document.getElementById('txtCompleteTime').value = '{0}';</script>", "");
                        Page.ClientScript.RegisterStartupScript(Page.GetType(), "", str);
                    }
                    //权限设置
                    int ieCount = objUserTeamRoleManager.GetUserInfoTeam(Session["FullName"].ToString(), 2, 4);
                    if(ieCount>0)
                    {
                        txtPs.Enabled = true;
                        string str = string.Format("<script>document.getElementById('txtCompleteTime').disabled = '{0}';</script>","disabled");
                        Page.ClientScript.RegisterStartupScript(Page.GetType(), "", str);
                    }
                    if (number == -1)
                    {
                        BtnSubmit.Enabled = false;
                        return;
                    }
                    int saleCount = objUserTeamRoleManager.GetUserInfoTeam(Session["FullName"].ToString(), 3, 1) + objUserTeamRoleManager.GetUserInfoTeam(Session["FullName"].ToString(), 3, 4);
                    if (saleCount > 0)
                    {
                        cblType.Enabled = true;
                        ddlCustomer.Enabled = true;
                        txtCustomer.Enabled = true;
                        //txtNo.Enabled = true;
                        txtType.Enabled = true;
                        ddlClass.Enabled = true;
                        txtRequirement.Enabled = true;
                        txtSales.Enabled = true;
                        ddlSuccess.Enabled = true;
                        txtMoldRequire.Enabled = true;
                        ddlMoldClass.Enabled = true;
                    }

                }
                catch (Exception ex)
                {
                    string msg = string.Format("<script>alert('{0}')</script>", ex.Message);
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                }
            }
        }

        protected void BtnSubmit_Click(object sender, EventArgs e)
        {
            string costType = "";
            for (int i = 0; i < cblType.Items.Count; i++)
            {
                if(cblType.Items[i].Selected)
                {
                    if(costType.Length==0)
                    { costType = cblType.Items[i].Text; }
                    else { costType = costType + "," + cblType.Items[i].Text; }
                }
            }
            no = objBaojiaManager.GetNewNumber();
            Baojia objBaojia = new Baojia
            {
                WorkParentId = workParentId,
                Number = no,
                Customer = ddlCustomer.SelectedValue == "其他" ? txtCustomer.Text.Trim().ToString() : ddlCustomer.SelectedValue,
                Class = ddlClass.SelectedValue,
                CostType = costType,
                OtherCostType = txtType.Text.Trim(),
                Requirement = txtRequirement.Text.Trim() == "" ? 0 : Convert.ToDouble(txtRequirement.Text.Trim()),
                Sales = txtSales.Text.Trim() == "" ? 0 : Convert.ToDouble(txtSales.Text.Trim()),
                Success = Convert.ToChar(ddlSuccess.SelectedValue),
                DisplayName = Session["DisplayName"].ToString(),
                MoldRequire= txtMoldRequire.Text.Trim(),
                MoldClass=ddlMoldClass.SelectedValue,
                CompleteTime= Convert.ToDateTime(comDate),
                Ps=txtPs.Text
            };
            int saleCount = objUserTeamRoleManager.GetUserInfoTeam(Session["FullName"].ToString(), 3, 1) + objUserTeamRoleManager.GetUserInfoTeam(Session["FullName"].ToString(), 3, 4);
            if (saleCount > 0)
            {
                objBaojia.CompleteTime = Request.Params.Get("txtCompleteTime") == "" ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(Request.Params.Get("txtCompleteTime"));
            }
            //objBaojia.Sales = Convert.ToDouble(txtSales.Text.Trim());
            //objBaojia.Number = this.txtNo.Text;
            //objBaojia.Class = ddlClass.SelectedValue;
            try
            {
                objBaojiaManager.InsertBaojia(objBaojia);
                objBaojiaManager.InsertBaojiaLog(objBaojia);
                txtNo.Text = no;
                foreach (var item in Enum.GetValues(typeof(Customer)))
                {
                    if (objBaojia.Customer == item.ToString())
                    {
                        ddlCustomer.SelectedValue = objBaojia.Customer;
                        otherCustmoer.Style.Value = "display:none";
                        break;
                    }
                    else
                    {
                        ddlCustomer.SelectedValue = "其他";
                        txtCustomer.Text = objBaojia.Customer;
                        otherCustmoer.Style.Value = "";
                    }
                }
                string msg = string.Format("<script>alert('报价编号:{0}保存成功！')</script>",  no);
                string str = string.Format("<script>window.close();</script>");
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "", str);
            }
            catch (Exception ex)
            {
                string msg = string.Format("<script>alert('{0}')</script>", ex.Message);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
            }
        }
    }

    enum Customer
    {
        UAES上海,
        UAES无锡,
        Bosch国外,
        Schneider,
        IRISO,
        TCS,
        Amphenol,
        AR,
        河北美泰,
        DELPHI,
        Bosch国内,
        TE,
        SIEMON,
        UAES芜湖,
        Hilite,
        Hella,
        湖州京昌,
        江苏奥利威,
        杭州乐荣,
        双林,
        西安昆仑,
        上海逸航,
        苏州汇川技术有限公司,
        天津斯巴克瑞,
        扬州奥力威,
        Valeo,
        华域皮尔博格,
        威伯科
    }
}